package com.iheima.dao;

import com.iheima.domain.User;

import java.util.ArrayList;
import java.util.List;

public class UserDao {
    private static List<User>users=new ArrayList<>();
    private static int id =0;
    static {
        users.add(new User(++id, "大郎1", "男", 33, "广东", "12345", "12345@qq.com"));
        users.add(new User(++id, "二郎", "男", 31, "广东", "12312", "12312@qq.com"));
        users.add(new User(++id, "张三", "女", 23, "广东", "54321", "54321@qq.com"));
        users.add(new User(++id, "李四", "男", 20, "广西", "12345", "12345@qq.com"));
        users.add(new User(++id, "王五", "男", 24, "广西", "55555", "55555@qq.com"));
        users.add(new User(++id, "赵六", "男", 34, "广西", "66666", "66666@qq.com"));
        users.add(new User(++id, "钱七", "女", 22, "广西", "77777", "77777@qq.com"));
        users.add(new User(++id, "张三丰", "男", 122, "湖南", "77779", "77779@qq.com"));
        users.add(new User(++id, "张无忌", "男", 22, "湖南", "77778", "77778@qq.com"));
        users.add(new User(++id, "张翠山", "男", 42, "湖南", "77776", "77776@qq.com"));

        users.add(new User(++id, "猪八戒", "男", 25, "广东", "834523234", "zhuzhuxia@itcast.cn"));
        users.add(new User(++id, "貂蝉", "女", 18, "湖南", "59869834", "diaochan@qq.com"));
        users.add(new User(++id, "孙悟空", "男", 28, "湖南", "87967822", "wukong@itheima.com"));
        users.add(new User(++id, "周瑜", "男", 25, "广西", "2743759345", "zhou@163.com"));
        users.add(new User(++id, "陈宏子", "男", 21, "广东", "15811688", "cheng@126.com"));
        users.add(new User(++id, "秦婷婷", "女", 25, "广东", "15888802", "zhangsan1@126.com"));
        users.add(new User(++id, "折蓉蓉", "女", 30, "广东", "13423333", "zherong@163.com"));
        users.add(new User(++id, "曹丽娜", "女", 26, "广东", "12223333", "zhangsan1@qq.com"));
        users.add(new User(++id, "李娜", "女", 46, "广西", "1343333", "zhangsan1@itcast.cn"));
        users.add(new User(++id, "姜钰", "男", 49, "湖南", "13223333", "zhangsan1@qq.com"));
        users.add(new User(++id, "魏天霞", "女", 38, "湖南", "13054863", "zhangsan1@126.com"));
        users.add(new User(++id, "王官君", "男", 22, "湖南", "1422233", "zhangsan1@126.com"));
        users.add(new User(++id, "王红梅", "女", 25, "湖南", "2777726", "wang@qq.com"));
        users.add(new User(++id, "郭淑慧", "女", 28, "湖南", "6058168", "guoshu@qq.com"));
        users.add(new User(++id, "马海娟", "女", 29, "湖南", "13829882", "zhangsan1@itcast.cn"));
        users.add(new User(++id, "孙洪山", "男", 24, "广东", "1393518", "zhangsan1@gmail.com"));
        users.add(new User(++id, "胡丹丹", "女", 19, "广西", "132138998", "zhangsan1@itcast.cn"));
        users.add(new User(++id, "胡书琴", "女", 30, "广西", "152020988", "zhangsan1@126.com"));
        users.add(new User(++id, "冷静", "男", 32, "广东", "13422223", "lengjing@gmail.com"));
        users.add(new User(++id, "李剑", "男", 33, "湖南", "13423333", "lijian@126.com"));
        users.add(new User(++id, "任娟娟", "女", 21, "广西", "13636668", "juanjuan@itcast.cn"));
        users.add(new User(++id, "范志坚", "男", 22, "湖南", "1223333", "zhijain@126.com"));
        users.add(new User(++id, "王继开", "男", 21, "海南", "1685168", "jikai@itcast.cn"));
        users.add(new User(++id, "刘小超", "男", 21, "广东", "1342333", "xiaochao@gmail.com"));
    }
    public List<User> queryAll() {
        return users;
    }

    public User chekname(String name) {
        for (User user : users) {
            if(user.getName().equals(name)){
                return user;
            }
        }
        return null;
    }

    public void add(User user) {
        user.setId(++id);
        users.add(user);
    }

    public User findById(int id) {
        for (User user : users) {
            if(id==user.getId()){
                return user;
            }
        }
        return null;
    }

    public void edit(User user) {
        for (int i = 0; i < users.size(); i++) {
            if(user.getId()==users.get(i).getId()){
                users.set(i,user);
                break;
            }
        }
    }

    public void delete(int id) {
        for (User user : users) {
            if(user.getId()==id){
                users.remove(user);
                break;
            }
        }
    }

    public int totalCount() {
        return users.size();
    }

    public List<User> pageQuery(int startIndex, int pageSize) {
        int endIndex = startIndex+pageSize;

        if(endIndex>=users.size()){
            return users.subList(startIndex, users.size());
        }else {
            return users.subList(startIndex,endIndex);
        }
    }
}

